/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utain.xmlfilesearcher2;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author Nut
 */
public class UtainXMLFileSearcher2 {

    static String tmp;

    public static void main(String[] args) {
        // TODO code application logic here
        UtainXMLFileSearcher2 uXmlFS2 = new UtainXMLFileSearcher2();
        try {
            String[] sh = uXmlFS2.getKeyWord();
            uXmlFS2.checkXML(sh);
        } catch (Exception e) {
        }
    }

    private String[] getKeyWord() throws Exception {
        ArrayList<String> list = new ArrayList<String>();
        BufferedReader br = new BufferedReader(new FileReader("key.txt"));
        String tmp;
        while ((tmp = br.readLine()) != null) {
            list.add(tmp);
        }
        br.close();

        String[] StringArray = new String[list.size()];
        StringArray = list.toArray(StringArray);

        return StringArray;
    }
    static boolean bFound = false;
    static boolean wFound = false;
    static boolean qFound = false;
    static boolean searched = false;
    ArrayList<String> liGet;

    private void checkXML(String[] key) throws Exception {
        File fXmlFile = new File("quotes.xml");
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader reader = factory.createXMLEventReader(
                new BufferedInputStream(new FileInputStream(fXmlFile)));
        liGet = new ArrayList<String>();
        String eName;

        while (reader.hasNext()) {
            XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                StartElement element = (StartElement) event;
                eName = element.getName().getLocalPart();
                if (eName.equals("quote")) {
                    qFound = true;
                }
                if (qFound && eName.equals("words")) {
                    wFound = true;
                }
                if (qFound && eName.equals("by")) {
                    bFound = true;
                }
            }
            if (event.isEndElement()) {
                EndElement element = (EndElement) event;
                eName = element.getName().getLocalPart();
                if (eName.equals("quote")) {
                    qFound = false;
                }
                if (qFound && eName.equals("words")) {
                    wFound = false;
                }
                if (qFound && eName.equals("by")) {
                    bFound = false;
                }
            }
            if (event.isCharacters()) {
                Characters chares = (Characters) event;
                for (String key1 : key) {
                    if (wFound) {
                        tmp = chares.getData();
                        searched = false;
                    }
                    if (bFound && chares.getData().contains(key1)) {
                        liGet.add(tmp + chares.getData());
                        searched = true;
                    }
                }
            }
        }
        reader.close();
        Collections.reverse(liGet);//if want to revers
        for (String xxx : liGet) {
            System.out.println(xxx);
        }
    }
}
